home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / PartMaker 4.3 / EventLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-12  |  3.7 KB  |  139 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** File:        EventLoop.c
  5. ** Written by:    Eric Soldan
  6. **
  7. ** Copyright © 1990-1993 Apple Computer, Inc.
  8. ** All rights reserved.
  9. */
  10.  
  11. /* You may incorporate this sample code into your applications without
  12. ** restriction, though the sample code has been provided "AS IS" and the
  13. ** responsibility for its operation is 100% yours.  However, what you are
  14. ** not permitted to do is to redistribute the source as "DSC Sample Code"
  15. ** after having made changes. If you're going to re-distribute the source,
  16. ** we require that you make it clear in the source that the code was
  17. ** descended from Apple Sample Code, but that you've made changes. */
  18.  
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24.  
  25. #include "App.h"            /* Get the application includes/typedefs, etc.    */
  26. #include "App.protos.h"        /* Get the prototypes for application.            */
  27.  
  28.  
  29.  
  30. /*****************************************************************************/
  31.  
  32.  
  33.  
  34. extern RgnHandle    gCursorRgn;                /* DTS.Lib..framework global. */
  35.     /* The current cursor region.  The initial cursor region is an empty region,
  36.     ** which will cause WaitNextEvent to generate a mouse-moved event, which will
  37.     ** cause us to set the cursor for the first time. */
  38.  
  39. extern Boolean        gQuitApplication;        /* DTS.Lib..framework global. */
  40.     /* This is set to false by Initialize.  When the user selects Quit
  41.     ** (and does not abort the quit), then this boolean is set true. */
  42.  
  43. Boolean        gLowOnMem;
  44. static void    ManageLowMem(void);
  45.  
  46. #define kRamReserve 0x10000L
  47. #define kRamSlop    0x08000L
  48.  
  49.  
  50.  
  51. /*****************************************************************************/
  52. /*****************************************************************************/
  53.  
  54. #ifdef applec
  55. #pragma segment EventLoop
  56. #endif
  57.  
  58. /*****************************************************************************/
  59. /*****************************************************************************/
  60.  
  61.  
  62.  
  63. /* Get events forever, and handle them by calling DoEvent.  Get the events by
  64. ** calling WaitNextEvent. */
  65.  
  66. void    EventLoop(void)
  67. {
  68.     EventRecord        event;
  69.  
  70.     while (!gQuitApplication) {
  71.         ManageLowMem();
  72.         if (!WaitNextEvent(everyEvent, &event, 15, gCursorRgn))
  73.             event.what = nullEvent;
  74.         DoEvent(&event);
  75.     };
  76. }
  77.  
  78.  
  79.  
  80. /*****************************************************************************/
  81.  
  82.  
  83.  
  84. /* This is called to determine if there is enough ram available for various
  85. ** application operations.  Typically, if there is not enough, certain menu
  86. ** items are dimmed, thus preventing further use of memory. */
  87.  
  88. static void    ManageLowMem(void)
  89. {
  90.     static Handle    reserveMemHndl;
  91.  
  92.     if (!reserveMemHndl) {
  93.         reserveMemHndl = NewHandle(0);
  94.         EmptyHandle(reserveMemHndl);
  95.     }
  96.  
  97.     if (gLowOnMem) {
  98.         ReallocateHandle(reserveMemHndl, kRamReserve + kRamSlop);
  99.         if (*reserveMemHndl) {
  100.             SetHandleSize(reserveMemHndl, kRamReserve);
  101.             HPurge(reserveMemHndl);
  102.             gLowOnMem = false;
  103.         }
  104.     }
  105.     else {
  106.         if (!*reserveMemHndl) {
  107.             ReallocateHandle(reserveMemHndl, kRamReserve);
  108.             if (*reserveMemHndl) HPurge(reserveMemHndl);
  109.             else {
  110.                 NewDocumentWindow(nil, 'LMEM', false);
  111.                 gLowOnMem = true;
  112.             }
  113.         }
  114.     }
  115. }
  116.  
  117.  
  118.  
  119. /*****************************************************************************/
  120. /*****************************************************************************/
  121. /*****************************************************************************/
  122.  
  123.  
  124.  
  125. /* •• Called by DTS.Lib..framework. •• */
  126.  
  127. /* Handle the cursor changes.  Most of the work is done by the application
  128. ** framework.  Each window has its own cursor calculation proc, so you
  129. ** shouldn't have to add any code here.  You may wish to do some special
  130. ** stuff prior to the DoWindowCursor call if you have modeless dialogs. */
  131.  
  132. void    DoCursor(void)
  133. {
  134.     DoWindowCursor();
  135. }
  136.  
  137.  
  138.  
  139.